package org.zoomdev.zoom;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CountBasedDiscountSystem {

    // 使用示例
    public static void main(String[] args) {
        // 配置优惠规则：每天9:00-11:00，最多允许2次优惠
        DiscountRule morningRule = new DiscountRule(
                LocalTime.of(9, 0),
                LocalTime.of(11, 0),
                2
        );

        DiscountService service = new DiscountService();

        // 模拟用户消费记录
        String userId = "user_001";
        service.addRecord(new ConsumptionRecord(userId,
                LocalDateTime.of(2023, 10, 1, 9, 30)));
        service.addRecord(new ConsumptionRecord(userId,
                LocalDateTime.of(2023, 10, 1, 10, 15)));

        // 测试第三次消费
        boolean available = service.isDiscountAvailable(userId, morningRule);
        System.out.println("第三次消费优惠可用? " + available); // 输出: false

        // 测试跨天时间段
        DiscountRule nightRule = new DiscountRule(
                LocalTime.of(22, 0),
                LocalTime.of(2, 0),
                1
        );

        service.addRecord(new ConsumptionRecord(userId,
                LocalDateTime.of(2023, 10, 1, 23, 45)));

        available = service.isDiscountAvailable(userId, nightRule);
        System.out.println("跨天时段优惠可用? " + available); // 输出: false
    }

    // 消费记录实体
    static class ConsumptionRecord {
        private final String userId;
        private final LocalDateTime consumeTime;

        public ConsumptionRecord(String userId, LocalDateTime consumeTime) {
            this.userId = userId;
            this.consumeTime = consumeTime;
        }

        public String getUserId() {
            return userId;
        }

        public LocalDateTime getConsumeTime() {
            return consumeTime;
        }
    }

    // 优惠规则配置
    static class DiscountRule {
        private final LocalTime startTime;
        private final LocalTime endTime;
        private final int maxCount;

        public DiscountRule(LocalTime startTime, LocalTime endTime, int maxCount) {
            this.startTime = startTime;
            this.endTime = endTime;
            this.maxCount = maxCount;
        }

        public boolean isWithinTimeRange(LocalTime time) {
            if (startTime.isBefore(endTime)) {
                return !time.isBefore(startTime) && !time.isAfter(endTime);
            } else { // 处理跨天时间段
                return !time.isBefore(startTime) || !time.isAfter(endTime);
            }
        }
    }

    // 优惠服务核心逻辑
    static class DiscountService {
        private final List<ConsumptionRecord> records = new ArrayList<>();

        public void addRecord(ConsumptionRecord record) {
            records.add(record);
        }

        public boolean isDiscountAvailable(String userId, DiscountRule rule) {
            // 获取用户当天所有相关消费记录
            List<ConsumptionRecord> userRecords = records.stream()
                    .filter(r -> r.getUserId().equals(userId))
                    .filter(r -> isInEffectivePeriod(r.getConsumeTime(), rule))
                    .collect(Collectors.toList());

            return userRecords.size() < rule.maxCount;
        }

        private boolean isInEffectivePeriod(LocalDateTime time, DiscountRule rule) {
            LocalTime localTime = time.toLocalTime();

            // 构建正确的时间段
            if (rule.startTime.isBefore(rule.endTime)) {
                return localTime.isAfter(rule.startTime) &&
                        localTime.isBefore(rule.endTime);
            } else { // 处理跨天情况
                return localTime.isAfter(rule.startTime) ||
                        localTime.isBefore(rule.endTime);
            }
        }
    }
}